package com.liukai.todo_android.ui.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.liukai.todo_android.R;
import com.liukai.todo_android.call_back.IUpdateScheduleCallBack;
import com.liukai.todo_android.common.utils.Constants;
import com.liukai.todo_android.entity.ItemVo;
import com.liukai.todo_android.entity.Resp;
import com.liukai.todo_android.entity.vo.UpdateItemVo;
import com.liukai.todo_android.presenter.UpdateSchedulePresenter;
import com.liukai.todo_android.ui.dialog.listener.ModifySuccessListener;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.Date;

/**
 * @author ：liukai
 * @date ：Created in 2021/8/13 5:36 下午
 * @description：新增待办的对话框
 * @modified By：
 * @version: $
 */
public class SaveConcentrationDialog implements IUpdateScheduleCallBack {

    private ItemVo itemVo;
    private Context context;
    private final int layoutId = R.layout.dialog_save_concentration;
    private View view;
    private AlertDialog dialog;
    private TextView btnSave;
    private TextView btnCancel;
    private EditText etSummary;
    private UpdateSchedulePresenter updateSchedulePresenter;
    private AVLoadingIndicatorView avi;
    private EditText etScheduleTitle;
    private EditText etScheduleDesc;
    private AlertDialog warningDialog;

    public SaveConcentrationDialog(Context context, ItemVo _itemVo) {
        this.context = context;
        this.itemVo = _itemVo;
        createDialog();
        initView();
        initDate();
        initListener();
        createWarningDialog();

    }

    private void initDate() {
        updateSchedulePresenter = new UpdateSchedulePresenter();
        updateSchedulePresenter.registerCallback(this);
        etScheduleTitle.setText(itemVo.getTitle());
        etScheduleDesc.setText(itemVo.getDescription());
    }

    private void initListener() {
        btnSave.setOnClickListener(v -> {
            saveThisConcentration();
            avi.setVisibility(View.VISIBLE);
            avi.show();
        });
        btnCancel.setOnClickListener(v -> {
            warningDialog.show();
        });
    }

    private void saveThisConcentration() {
        String summary = etSummary.getText().toString();
        if (summary.equals("")) {
            Toast.makeText(context, "总结不能为空", Toast.LENGTH_SHORT).show();
            avi.hide();
            avi.setVisibility(View.GONE);
            return;
        }
        itemVo.setSummary(summary);
        updateSchedulePresenter.saveConcentration(itemVo);
    }

    private void initView() {
        btnSave = view.findViewById(R.id.btn_save);
        btnCancel = view.findViewById(R.id.btn_cancel);
        etSummary = view.findViewById(R.id.et_summary);
        avi = view.findViewById(R.id.avi);
        etScheduleTitle = view.findViewById(R.id.schedule_title);
        etScheduleDesc = view.findViewById(R.id.schedule_desc);
    }

    /**
     * 创建新增对话框
     */
    private void createDialog() {
        view = View.inflate(context, layoutId, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        // 设置点击不可取消
        builder.setCancelable(false);
        dialog = builder.create();
    }

    public void show() {
        dialog.show();
        // 获取Window对象
        Window window = dialog.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        // 设置显示视图内容
        window.setContentView(view);
    }

    private void createWarningDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("注意!");
        builder.setMessage("关闭此窗口将不会保存此次专注记录！");
        builder.setCancelable(false);
        builder.setPositiveButton("取消", (dialog, which) -> warningDialog.dismiss());
        builder.setNeutralButton("退出专注", (dialog, which) -> {
            warningDialog.dismiss();
            modifySuccessListener.cancel();
        });
        warningDialog = builder.create();

    }


    @Override
    public void updateScheduleSuccess(Resp resp) {
        modifySuccessListener.saveSummarySuccess();
    }

    @Override
    public void updateScheduleError(Resp resp) {

    }

    private ModifySuccessListener modifySuccessListener;

    public void setModifySuccessListener(ModifySuccessListener modifySuccessListener) {
        this.modifySuccessListener = modifySuccessListener;
    }
}
